package com.intelligentmeterreadingsys.usermanager.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.intelligentmeterreadingsys.BaseDao.BaseDao;
import com.intelligentmeterreadingsys.povo.UseWaterInfoPoVo;
import com.intelligentmeterreadingsys.povo.UserinfoVO;

/**
 * 用户信息查询数据交换
 * @author king
 *
 */
public class UserInfoDao extends BaseDao{

	public SQLiteDatabase db;
	public UserInfoDao(Context context) {
		super(context);
		this.db=super.getDb();
		
	}
	
	/**
	 * 查询用户历史用水量
	 * @param usernumber
	 * @return
	 */
	public List<UseWaterInfoPoVo> queryUseWater(String usernumber){
		if(TextUtils.isEmpty(usernumber)){
			System.out.println("-------无");
			return null;
		}
		StringBuffer sb=new StringBuffer("select u.[waternumber],u.[month],u.[watercount]  ");
		sb.append("from  usewaterinfo u,waterinfo w where w.[waternumber]=u.[waternumber]");
		sb.append(" and w.[usernumber]='").append(usernumber).append("' order by u.[month]");
		String sql=sb.toString();
		System.out.println(sql);
		Cursor cursor=db.rawQuery(sql, null);
		List<UseWaterInfoPoVo> list=new ArrayList<UseWaterInfoPoVo>();
		String month="";
		
		while(cursor.moveToNext()){
			String mo=cursor.getString(cursor.getColumnIndex("month"));
			System.out.println(mo);
			Double count=Double.parseDouble(cursor.getString(cursor.getColumnIndex("watercount")));
			System.out.println("-------------"+count);
			if(mo.equals(month)){
				System.out.println("月份相同");
				UseWaterInfoPoVo use=list.get(list.size()-1);
				use.setWatercount(use.getWatercount()+count);
				System.out.println(use.getWatercount()+"----------");
				list.remove(list.size()-1);
				list.add(use);
				month=mo;
			}else{
				System.out.println("月份不同");
				UseWaterInfoPoVo usewater=new UseWaterInfoPoVo();
				usewater.setMoth(mo);
				usewater.setWatercount(count);
				
				list.add(usewater);
				month=mo;
				
			}
		
			
		}
		if(cursor!=null){
			cursor.close();
		}
		return list;
		
	}
	
	/**
	 *  查询用户信息
	 * @param name
	 * @param adds
	 * @param phone
	 * @param usernumber
	 * @param usertype
	 * @return
	 */
	public List<UserinfoVO> queryUserInfo(String name, String adds,
			String phone, String usernumber, String usertype) {
		StringBuffer sb=new StringBuffer("select u.[_id],u.[username],u.[useradds],u.[usertype],u.[phone],u.[usernumber],u.[userstatus]");
	    sb.append(" from userinfo u where 0=0");
		if(!TextUtils.isEmpty(name)){
			sb.append(" and u.[username]='").append(name).append("'");
		}
		if(!TextUtils.isEmpty(adds)){
			sb.append(" and u.[useradds] like '").append(adds).append("'");
		}
		if(!TextUtils.isEmpty(phone)){
			sb.append(" and u.[phone]='").append(phone).append("'");
		}
		if(!TextUtils.isEmpty(usernumber)){
			sb.append(" and u.[usernumber]='").append(usernumber).append("'");
		}
		if(!TextUtils.isEmpty(usertype)&&!usertype.equals("全部")){
			
			sb.append(" and u.[usertype]='").append(usertype).append("'");
		}
		String sql=sb.toString();
		System.out.println(sql);
		Cursor cursor=db.rawQuery(sql, null);
		List<UserinfoVO> list=new ArrayList<UserinfoVO>();
		while(cursor.moveToNext()){
			UserinfoVO user=new UserinfoVO();
			user.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			user.setName(cursor.getString(cursor.getColumnIndex("username")));
			user.setAdds(cursor.getString(cursor.getColumnIndex("useradds")));
			user.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
			user.setType(cursor.getString(cursor.getColumnIndex("usertype")));
			user.setNumber(cursor.getString(cursor.getColumnIndex("usernumber")));
			user.setStatus(cursor.getString(cursor.getColumnIndex("userstatus")));
			list.add(user);
		}
		if(cursor!=null){
			cursor.close();
		}
		return list;
	}
	
	/**
	 * 关闭连接
	 */
	public void close(){
		if(db!=null&&db.isOpen()){
			db.close();
			super.close();
		}
	}

}
